﻿namespace CV.Controllers.WebApi
{
    using CV.Entities.Dto;
    using CV.Interfaces.Bll;
    using System;
    using System.Collections.Generic;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;

    public class UserController : ApiController
    {
        private IUserService userService;


        public UserController(IUserService userService)
        {
            this.userService = userService;
        }

        // GET: api/User
        public IEnumerable<User> Get()
        {
            return userService.ListAll();
        }

        // GET: api/User/5
        public HttpResponseMessage Get(Guid id)
        {
            var result = userService.GetUserById(id);

            if (result == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound, "Not found");
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.OK, result);
            }

        }

        // POST: api/User
        public HttpResponseMessage Post([FromBody]User user)
        {
            var createdUser = userService.Create(user);
            var response = Request.CreateResponse(HttpStatusCode.Created);
            response.Headers.Location = new Uri(Request.RequestUri + "/" + createdUser.Id);
            return response;
        }

        // PUT: api/User
        public void Put([FromBody]User user)
        {
            userService.Update(user);
        }

        // DELETE: api/User/5
        public void Delete(Guid id)
        {
            userService.Delete(id);
        }
    }
}
